#include <ebase/types.h>
#include <ebase/trace.h>
#include <ebase/builtins.h>

#include <common/return_codes.h>

#include "isi.h"
#include "isi_iss.h"
#include "isi_priv.h"
#include "SP2518_priv.h"

//AE
#define  SP2518_P0_0xf7  0x80//78
#define  SP2518_P0_0xf8  0x74//6e
#define  SP2518_P0_0xf9  0x80//74
#define  SP2518_P0_0xfa  0x74//6a
//HEQ
#define  SP2518_P0_0xdd  0x80
#define  SP2518_P0_0xde  0x95
//auto lum
#define SP2518_NORMAL_Y0ffset  	  0x10	//0x0f	 modify by sp_yjp,20120813
#define SP2518_LOWLIGHT_Y0ffset  0x20

/*****************************************************************************
 * DEFINES
 *****************************************************************************/


/*****************************************************************************
 * GLOBALS
 *****************************************************************************/

// Image sensor register settings default values taken from data sheet OV8810_DS_1.1_SiliconImage.pdf.
// The settings may be altered by the code in IsiSetupSensor.
const IsiRegDescription_t SP2518_g_aRegDescription[] =
{
	{0xfd,0x00,"",eReadWrite},
	{0x1b,0x1a,"",eReadWrite},
	{0x0e,0x01,"",eReadWrite},

	{0x0f,0x2f,"",eReadWrite},
	{0x10,0x2e,"",eReadWrite},
	{0x11,0x00,"",eReadWrite},
	{0x12,0x4f,"",eReadWrite},
	{0x14,0x40,"",eReadWrite},
	{0x16,0x02,"",eReadWrite},
	{0x17,0x10,"",eReadWrite},
	{0x1a,0x1f,"",eReadWrite},
	{0x1e,0x81,"",eReadWrite},
	{0x21,0x00,"",eReadWrite},
	{0x22,0x1b,"",eReadWrite},
	{0x25,0x10,"",eReadWrite},
	{0x26,0x25,"",eReadWrite},
	{0x27,0x6d,"",eReadWrite},
	{0x2c,0x23,"",eReadWrite},
	{0x2d,0x75,"",eReadWrite},
	{0x2e,0x38,"",eReadWrite},
	{0x31,0x10,"",eReadWrite},
	{0x44,0x03,"",eReadWrite},
	{0x6f,0x00,"",eReadWrite},
	{0xa0,0x04,"",eReadWrite},
	{0x5f,0x01,"",eReadWrite},
	{0x32,0x00,"",eReadWrite},
	{0xfd,0x01,"",eReadWrite},
	{0x2c,0x00,"",eReadWrite},
	{0x2d,0x00,"",eReadWrite},
	{0xfd,0x00,"",eReadWrite},
	{0xfb,0x83,"",eReadWrite},
	{0xf4,0x09,"",eReadWrite},
	//Pregain
	{0xfd,0x01,"",eReadWrite},
	{0xc6,0x90,"",eReadWrite},
	{0xc7,0x90,"",eReadWrite},
	{0xc8,0x90,"",eReadWrite},
	{0xc9,0x90,"",eReadWrite},
	//blacklevel
	{0xfd,0x00,"",eReadWrite},
	{0x65,0x08,"",eReadWrite},
	{0x66,0x08,"",eReadWrite},
	{0x67,0x08,"",eReadWrite},
	{0x68,0x08,"",eReadWrite},
	//bpc
	{0x46,0xff,"",eReadWrite},
	//rpc
	{0xfd,0x00,"",eReadWrite},
	{0xe0,0x6c,"",eReadWrite},
	{0xe1,0x54,"",eReadWrite},
	{0xe2,0x48,"",eReadWrite},
	{0xe3,0x40,"",eReadWrite},
	{0xe4,0x40,"",eReadWrite},
	{0xe5,0x3e,"",eReadWrite},
	{0xe6,0x3e,"",eReadWrite},
	{0xe8,0x3a,"",eReadWrite},
	{0xe9,0x3a,"",eReadWrite},
	{0xea,0x3a,"",eReadWrite},
	{0xeb,0x38,"",eReadWrite},
	{0xf5,0x38,"",eReadWrite},
	{0xf6,0x38,"",eReadWrite},
	{0xfd,0x01,"",eReadWrite},
	{0x94,0xc0,"",eReadWrite},
	{0x95,0x38,"",eReadWrite},
	{0x9c,0x6c,"",eReadWrite},
	{0x9d,0x38,"",eReadWrite},	
//#if 1
	/*24*3pll 13-13fps 50hz*/	
	{0xfd,0x00,"",eReadWrite},
	{0x03,0x03,"",eReadWrite},
	{0x04,0xf6,"",eReadWrite},
	{0x05,0x00,"",eReadWrite},
	{0x06,0x00,"",eReadWrite},
	{0x07,0x00,"",eReadWrite},
	{0x08,0x00,"",eReadWrite},
	{0x09,0x00,"",eReadWrite},
	{0x0a,0x8b,"",eReadWrite},
	{0x2f,0x00,"",eReadWrite},
	{0x30,0x08,"",eReadWrite},
	{0xf0,0xa9,"",eReadWrite},
	{0xf1,0x00,"",eReadWrite},
	{0xfd,0x01,"",eReadWrite},
	{0x90,0x07,"",eReadWrite},
	{0x92,0x01,"",eReadWrite},
	{0x98,0xa9,"",eReadWrite},
	{0x99,0x00,"",eReadWrite},
	{0x9a,0x01,"",eReadWrite},
	{0x9b,0x00,"",eReadWrite},
	{0xfd,0x01,"",eReadWrite},
	{0xce,0x9f,"",eReadWrite},
	{0xcf,0x04,"",eReadWrite},
	{0xd0,0x9f,"",eReadWrite},
	{0xd1,0x04,"",eReadWrite},
	{0xd7,0xa5,"",eReadWrite},
	{0xd8,0x00,"",eReadWrite},
	{0xd9,0xa9,"",eReadWrite},
	{0xda,0x00,"",eReadWrite},
	{0xfd,0x00,"",eReadWrite},
	//#endif
	{0xfd,0x01,"",eReadWrite},
	{0xca,0x30,"",eReadWrite}, //mean dummy2low
	{0xcb,0x50,"",eReadWrite}, //mean low2dummy
	{0xcc,0xc0,"",eReadWrite}, //f8;rpc low
	{0xcd,0xc0,"",eReadWrite}, //rpc dummy
	{0xd5,0x80,"",eReadWrite}, //mean normal2dummy
	{0xd6,0x90,"",eReadWrite}, //mean dummy2normal
	{0xfd,0x00,"",eReadWrite},  
	
	//lens shading 
	{0xfd,0x00,"",eReadWrite},
	{0xa1,0x20,"",eReadWrite},
	{0xa2,0x20,"",eReadWrite},
	{0xa3,0x20,"",eReadWrite},
	{0xa4,0xff,"",eReadWrite},
	{0xa5,0x80,"",eReadWrite},
	{0xa6,0x80,"",eReadWrite},
	{0xfd,0x01,"",eReadWrite},
	{0x64,0x1e,"",eReadWrite},//28
	{0x65,0x1c,"",eReadWrite},//25
	{0x66,0x1c,"",eReadWrite},//2a
	{0x67,0x16,"",eReadWrite},//25
	{0x68,0x1c,"",eReadWrite},//25
	{0x69,0x1c,"",eReadWrite},//29
	{0x6a,0x1a,"",eReadWrite},//28
	{0x6b,0x16,"",eReadWrite},//20
	{0x6c,0x1a,"",eReadWrite},//22
	{0x6d,0x1a,"",eReadWrite},//22
	{0x6e,0x1a,"",eReadWrite},//22
	{0x6f,0x16,"",eReadWrite},//1c
	{0xb8,0x04,"",eReadWrite},//0a
	{0xb9,0x13,"",eReadWrite},//0a
	{0xba,0x00,"",eReadWrite},//23
	{0xbb,0x03,"",eReadWrite},//14
	{0xbc,0x03,"",eReadWrite},//08
	{0xbd,0x11,"",eReadWrite},//08
	{0xbe,0x00,"",eReadWrite},//12
	{0xbf,0x02,"",eReadWrite},//00
	{0xc0,0x04,"",eReadWrite},//05
	{0xc1,0x0e,"",eReadWrite},//05
	{0xc2,0x00,"",eReadWrite},//18
	{0xc3,0x05,"",eReadWrite},//08   
	//raw filter
	{0xfd,0x01,"",eReadWrite},
	{0xde,0x0f,"",eReadWrite},
	{0xfd,0x00,"",eReadWrite},
	{0x57,0x08,"",eReadWrite},//raw_dif_thr
	{0x58,0x08,"",eReadWrite},//a
	{0x56,0x08,"",eReadWrite},//a
	{0x59,0x10,"",eReadWrite},

	{0x5a,0xa0,"",eReadWrite},//raw_rb_fac_outdoor
	{0xc4,0xa0,"",eReadWrite},//60raw_rb_fac_indoor
	{0x43,0xa0,"",eReadWrite},//40raw_rb_fac_dummy  
	{0xad,0x40,"",eReadWrite},//raw_rb_fac_low  

	{0x4f,0xa0,"",eReadWrite},//raw_gf_fac_outdoor
	{0xc3,0xa0,"",eReadWrite},//60raw_gf_fac_indoor
	{0x3f,0xa0,"",eReadWrite},//40raw_gf_fac_dummy
	{0x42,0x40,"",eReadWrite},//raw_gf_fac_low
	{0xc2,0x15,"",eReadWrite},

	{0xb6,0x80,"",eReadWrite},//raw_gflt_fac_outdoor
	{0xb7,0x80,"",eReadWrite},//60raw_gflt_fac_normal
	{0xb8,0x40,"",eReadWrite},//40raw_gflt_fac_dummy
	{0xb9,0x20,"",eReadWrite},//raw_gflt_fac_low

	{0xfd,0x01,"",eReadWrite},
	{0x50,0x0c,"",eReadWrite},//raw_grgb_thr
	{0x51,0x0c,"",eReadWrite},
	{0x52,0x10,"",eReadWrite},
	{0x53,0x10,"",eReadWrite},
	{0xfd,0x00,"",eReadWrite},	
	// awb1
	{0xfd,0x01,"",eReadWrite},
	{0x11,0x10,"",eReadWrite},
	{0x12,0x1f,"",eReadWrite},
	{0x16,0x1c,"",eReadWrite},
	{0x18,0x00,"",eReadWrite},
	{0x19,0x00,"",eReadWrite},
	{0x1b,0x96,"",eReadWrite},
	{0x1a,0x9a,"",eReadWrite},//95
	{0x1e,0x2f,"",eReadWrite},
	{0x1f,0x29,"",eReadWrite},
	{0x20,0xff,"",eReadWrite},
	{0x22,0xff,"",eReadWrite},  
	{0x28,0xce,"",eReadWrite},
	{0x29,0x8a,"",eReadWrite},
	{0xfd,0x00,"",eReadWrite},
	{0xe7,0x03,"",eReadWrite},
	{0xe7,0x00,"",eReadWrite},
	{0xfd,0x01,"",eReadWrite},
	{0x2a,0xf0,"",eReadWrite},
	{0x2b,0x10,"",eReadWrite},
	{0x2e,0x04,"",eReadWrite},
	{0x2f,0x18,"",eReadWrite},
	{0x21,0x60,"",eReadWrite},
	{0x23,0x60,"",eReadWrite},
	{0x8b,0xab,"",eReadWrite},
	{0x8f,0x12,"",eReadWrite},
	//awb2
	{0xfd,0x01,"",eReadWrite},
	{0x1a,0x80,"",eReadWrite},
	{0x1b,0x80,"",eReadWrite},
	{0x43,0x80,"",eReadWrite},
    //outdoor
    {0x00,0xd4,"",eReadWrite},
    {0x01,0xb0,"",eReadWrite},
    {0x02,0x90,"",eReadWrite},
    {0x03,0x78,"",eReadWrite},
	//d65
	{0x35,0xd6,"",eReadWrite},//d6;b0
	{0x36,0xf0,"",eReadWrite},//f0;d1;e9
	{0x37,0x7a,"",eReadWrite},//8a;70
	{0x38,0x9a,"",eReadWrite},//dc;9a;af
	//indoor
	{0x39,0xab,"",eReadWrite},
	{0x3a,0xca,"",eReadWrite},
	{0x3b,0xa3,"",eReadWrite},
	{0x3c,0xc1,"",eReadWrite},
	//f
	{0x31,0x82,"",eReadWrite},//7d
	{0x32,0xa5,"",eReadWrite},//a0;74
	{0x33,0xd6,"",eReadWrite},//d2
	{0x34,0xec,"",eReadWrite},//e8
	{0x3d,0xa5,"",eReadWrite},//a7;88
	{0x3e,0xc2,"",eReadWrite},//be;bb
	{0x3f,0xa7,"",eReadWrite},//b3;ad
	{0x40,0xc5,"",eReadWrite},//c5;d0
	//Color Correction				  
	{0xfd,0x01,"",eReadWrite},
	{0x1c,0xc0,"",eReadWrite},
	{0x1d,0x95,"",eReadWrite},
	{0xa0,0xa6,"",eReadWrite},//b8 
	{0xa1,0xda,"",eReadWrite},//;d5
	{0xa2,0x00,"",eReadWrite},//;f2
	{0xa3,0x06,"",eReadWrite},//;e8
	{0xa4,0xb2,"",eReadWrite},//;95
	{0xa5,0xc7,"",eReadWrite},//;03
	{0xa6,0x00,"",eReadWrite},//;f2
	{0xa7,0xce,"",eReadWrite},//;c4
	{0xa8,0xb2,"",eReadWrite},//;ca
	{0xa9,0x0c,"",eReadWrite},//;3c
	{0xaa,0x30,"",eReadWrite},//;03
	{0xab,0x0c,"",eReadWrite},//;0f
	{0xac,0xc0,"",eReadWrite},//b8 
	{0xad,0xc0,"",eReadWrite},//d5
	{0xae,0x00,"",eReadWrite},//f2
	{0xaf,0xf2,"",eReadWrite},//e8
	{0xb0,0xa6,"",eReadWrite},//95
	{0xb1,0xe8,"",eReadWrite},//03
	{0xb2,0x00,"",eReadWrite},//f2
	{0xb3,0xe7,"",eReadWrite},//c4
	{0xb4,0x99,"",eReadWrite},//ca
	{0xb5,0x0c,"",eReadWrite},//3c
	{0xb6,0x33,"",eReadWrite},//03
	{0xb7,0x0c,"",eReadWrite},//0f
	//Saturation
	{0xfd,0x00,"",eReadWrite},
	{0xbf,0x01,"",eReadWrite},
	{0xbe,0xbb,"",eReadWrite},
	{0xc0,0xb0,"",eReadWrite},
	{0xc1,0xf0,"",eReadWrite},
	
    {0xd3,0x77,"",eReadWrite},
    {0xd4,0x77,"",eReadWrite},
    {0xd6,0x77,"",eReadWrite},
    {0xd7,0x77,"",eReadWrite},
    {0xd8,0x77,"",eReadWrite},
    {0xd9,0x77,"",eReadWrite},
    {0xda,0x77,"",eReadWrite},
    {0xdb,0x77,"",eReadWrite},
	//uv_dif
	{0xfd,0x00,"",eReadWrite},
	{0xf3,0x03,"",eReadWrite},
	{0xb0,0x00,"",eReadWrite},
	{0xb1,0x23,"",eReadWrite},
	//gamma1
	{0xfd,0x00,"",eReadWrite},//
	{0x8b,0x0 ,"",eReadWrite},//0 ;0	
	{0x8c,0xA ,"",eReadWrite},//14;A 
	{0x8d,0x13,"",eReadWrite},//24;13
	{0x8e,0x25,"",eReadWrite},//3a;25
	{0x8f,0x43,"",eReadWrite},//59;43
	{0x90,0x5D,"",eReadWrite},//6f;5D
	{0x91,0x74,"",eReadWrite},//84;74
	{0x92,0x88,"",eReadWrite},//95;88
	{0x93,0x9A,"",eReadWrite},//a3;9A
	{0x94,0xA9,"",eReadWrite},//b1;A9
	{0x95,0xB5,"",eReadWrite},//be;B5
	{0x96,0xC0,"",eReadWrite},//c7;C0
	{0x97,0xCA,"",eReadWrite},//d1;CA
	{0x98,0xD4,"",eReadWrite},//d9;D4
	{0x99,0xDD,"",eReadWrite},//e1;DD
	{0x9a,0xE6,"",eReadWrite},//e9;E6
	{0x9b,0xEF,"",eReadWrite},//f1;EF
	{0xfd,0x01,"",eReadWrite},//01;01
	{0x8d,0xF7,"",eReadWrite},//f9;F7
	{0x8e,0xFF,"",eReadWrite},//ff;FF
	//gamma2   
	{0xfd,0x00,"",eReadWrite},//
	{0x78,0x0 ,"",eReadWrite},//0   
	{0x79,0xA ,"",eReadWrite},//14
	{0x7a,0x13,"",eReadWrite},//24
	{0x7b,0x25,"",eReadWrite},//3a
	{0x7c,0x43,"",eReadWrite},//59
	{0x7d,0x5D,"",eReadWrite},//6f
	{0x7e,0x74,"",eReadWrite},//84
	{0x7f,0x88,"",eReadWrite},//95
	{0x80,0x9A,"",eReadWrite},//a3
	{0x81,0xA9,"",eReadWrite},//b1
	{0x82,0xB5,"",eReadWrite},//be
	{0x83,0xC0,"",eReadWrite},//c7
	{0x84,0xCA,"",eReadWrite},//d1
	{0x85,0xD4,"",eReadWrite},//d9
	{0x86,0xDD,"",eReadWrite},//e1
	{0x87,0xE6,"",eReadWrite},//e9
	{0x88,0xEF,"",eReadWrite},//f1
	{0x89,0xF7,"",eReadWrite},//f9
	{0x8a,0xFF,"",eReadWrite},//ff

//gamma_ae  
	{0xfd,0x01,"",eReadWrite},
	{0x96,0x46,"",eReadWrite},
	{0x97,0x14,"",eReadWrite},
	{0x9f,0x06,"",eReadWrite},
	//HEQ
	{0xfd,0x00,"",eReadWrite},//
	{0xdd,SP2518_P0_0xdd,"",eReadWrite},//0x80	   
	{0xde,SP2518_P0_0xde,"",eReadWrite},//a0	
	{0xdf,0x80,"",eReadWrite},//
	//Ytarget 
	{0xfd,0x00,"",eReadWrite},// 
	{0xec,0x70,"",eReadWrite},//6a
	{0xed,0x86,"",eReadWrite},//7c
	{0xee,0x70,"",eReadWrite},//65
	{0xef,0x86,"",eReadWrite},//78
	{0xf7,0x80,"",eReadWrite},//78
	{0xf8,0x74,"",eReadWrite},//6e
	{0xf9,0x80,"",eReadWrite},//74
	{0xfa,0x74,"",eReadWrite},//6a 
	//sharpen
	{0xfd,0x01,"",eReadWrite},
	{0xdf,0x0f,"",eReadWrite},
	{0xe5,0x10,"",eReadWrite},
	{0xe7,0x10,"",eReadWrite},
	{0xe8,0x20,"",eReadWrite},
	{0xec,0x20,"",eReadWrite},
	{0xe9,0x20,"",eReadWrite},
	{0xed,0x20,"",eReadWrite},
	{0xea,0x10,"",eReadWrite},
	{0xef,0x10,"",eReadWrite},
	{0xeb,0x10,"",eReadWrite},
	{0xf0,0x10,"",eReadWrite},
	//;gw
	{0xfd,0x01,"",eReadWrite},//
	{0x70,0x76,"",eReadWrite},//
	{0x7b,0x40,"",eReadWrite},//
	{0x81,0x30,"",eReadWrite},//
	//;Y_offset
	{0xfd,0x00,"",eReadWrite},
	{0xb2,0X10,"",eReadWrite},
	{0xb3,0x1f,"",eReadWrite},
	{0xb4,0x30,"",eReadWrite},
	{0xb5,0x50,"",eReadWrite},
	//;CNR
	{0xfd,0x00,"",eReadWrite},
	{0x5b,0x20,"",eReadWrite},
	{0x61,0x80,"",eReadWrite},
	{0x77,0x80,"",eReadWrite},
	{0xca,0x80,"",eReadWrite},
	//;YNR  
	{0xab,0x00,"",eReadWrite},
	{0xac,0x02,"",eReadWrite},
	{0xae,0x08,"",eReadWrite},
	{0xaf,0x20,"",eReadWrite},
	{0xfd,0x00,"",eReadWrite},
	{0x31,0x10,"",eReadWrite},
	{0x32,0x0d,"",eReadWrite},
	{0x33,0xcf,"",eReadWrite},//ef
	{0x34,0x7f,"",eReadWrite},//3f
	
	{0x35,0x00,"",eReadWrite},//0x40        
	
	{0x1b,0x1a,"",eReadWrite},
	{0xe7,0x03,"",eReadWrite},
	{0xe7,0x00,"",eReadWrite},
	
	//SVGA
	{0xfd,0x00,"",eReadWrite},	

	{0x47,0x00,"",eReadWrite},
	{0x48,0x00,"",eReadWrite},
	{0x49,0x04,"",eReadWrite},
	{0x4a,0xb0,"",eReadWrite},
	
	{0x4b,0x00,"",eReadWrite},
	{0x4c,0x00,"",eReadWrite},	
	{0x4d,0x06,"",eReadWrite},
	{0x4e,0x40,"",eReadWrite},	
	{0xfd,0x01,"",eReadWrite},
	{0x06,0x00,"",eReadWrite},
	{0x07,0x40,"",eReadWrite},
	{0x08,0x00,"",eReadWrite},
	{0x09,0x40,"",eReadWrite},
	{0x0a,0x02,"",eReadWrite},	//600
	{0x0b,0x58,"",eReadWrite},
	{0x0c,0x03,"",eReadWrite},	//800
	{0x0d,0x20,"",eReadWrite},
	{0x0e,0x01,"",eReadWrite},
	{0xfd,0x00,"",eReadWrite},	
	{0x5d,0x0e,"",eReadWrite},	//vsync delay
    {0x0000 ,0x00,"eTableEnd",eTableEnd}

};

const IsiRegDescription_t SP2518_g_svga[] =
{

	{0xfd,0x00,"",eReadWrite},
	{0x47,0x00,"",eReadWrite},
	{0x48,0x00,"",eReadWrite},
	{0x49,0x04,"",eReadWrite},
	{0x4a,0xb0,"",eReadWrite},	
	{0x4b,0x00,"",eReadWrite},
	{0x4c,0x00,"",eReadWrite},	
	{0x4d,0x06,"",eReadWrite},
	{0x4e,0x40,"",eReadWrite},	
	{0xfd,0x01,"",eReadWrite},
	{0x06,0x00,"",eReadWrite},
	{0x07,0x40,"",eReadWrite},
	{0x08,0x00,"",eReadWrite},
	{0x09,0x40,"",eReadWrite},
	{0x0a,0x02,"",eReadWrite},	//600
	{0x0b,0x58,"",eReadWrite},
	{0x0c,0x03,"",eReadWrite},	//800
	{0x0d,0x20,"",eReadWrite},
	{0x0e,0x01,"",eReadWrite},
	{0xfd,0x00,"",eReadWrite},	//software wake
    {0x0000 ,0x00,"eTableEnd",eTableEnd}
};

const IsiRegDescription_t SP2518_g_1600x1200[] =
{
	{0xfd,0x00,"",eReadWrite},	
	{0x47,0x00,"",eReadWrite},
	{0x48,0x00,"",eReadWrite},
	{0x49,0x04,"",eReadWrite},
	{0x4a,0xb0,"",eReadWrite},
	
	{0x4b,0x00,"",eReadWrite},
	{0x4c,0x00,"",eReadWrite},	
	{0x4d,0x06,"",eReadWrite},
	{0x4e,0x40,"",eReadWrite},	

	{0xfd,0x01,"",eReadWrite},
	{0x0e,0x00,"",eReadWrite},
	{0xfd,0x00,"",eReadWrite},
    {0x0000 ,0x00,"eTableEnd",eTableEnd}

};


